package com.moyq5.mvc.framework.utils;

public class ByteUtils {

	private final static byte[] hex = "0123456789ABCDEF".getBytes();
	
	private ByteUtils() {}
	
	public static byte[] hex2Bytes(String hex) {
		byte[] b = new byte[hex.length() / 2];
		int j = 0;
		for (int i = 0; i < b.length; i++) {
			char c0 = hex.charAt(j++);
			char c1 = hex.charAt(j++);
			b[i] = (byte) ((parse(c0) << 4) | parse(c1));
		}
		return b;
	}

	public static String int2Hex(int a) {
		return Integer.toHexString(a);
	}

	public static String int2Hex(int a, int len) {
		String str = Integer.toHexString(a);
		return hex2ByteLength(str, len);
	}

	public static String long2Hex(long a) {
		return Long.toHexString(a);
	}

	public static String long2Hex(long a, int len) {
		String str = Long.toHexString(a);
		return hex2ByteLength(str, len);
	}

	public static String bytes2Hex(byte[] bs) {
		byte[] buff = new byte[2 * bs.length];
		for (int i = 0; i < bs.length; i++) {
			buff[2 * i] = hex[(bs[i] >> 4) & 0x0f];
			buff[2 * i + 1] = hex[bs[i] & 0x0f];
		}
		return new String(buff);
	}

	public static String bytes2Hex(byte[] bs, int len) {
		String str = bytes2Hex(bs);
		return hex2ByteLength(str, len);
	}

	public static boolean byte2Boolean(byte b, int bit) {
		return (bit & b) == bit;
	}

	public static char byte2Char(byte b) {
		return (char) b;
	}

	public static int byte2Int(byte b) {
		return b & 0xFF;
	}

	public static int bytes2Int(byte[] bs) {
		int v = 0;
		for (int i = 0; i < bs.length && i < 4; i++) {
			v |= (bs[i] & 0xFF) << (bs.length - i - 1) * 8;
		}
		return v;
		//return b[3] & 0xFF | (b[2] & 0xFF) << 8 | (b[1] & 0xFF) << 16 | (b[0] & 0xFF) << 24;
	}

	public static long bytes2Long(byte[] bs) {
		long v = 0;
		for (int i = 0; i < bs.length && i < 8; i++) {
			v |= (bs[i] & 0xFF) << (bs.length - i - 1) * 8;
		}
		return v;
		/*
		ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
		buffer.put(bs, 0, bs.length);
		buffer.flip();// need flip
		return buffer.getLong();
		*/
	}

	public static byte[] int2Bytes(int a) {
		byte[] bs = new byte[4];
		for (int i = 0; i < 4; i++) {
			bs[i] = (byte)((a >> ((3-i) * 8)) & 0xFF);
		}
		return bs;
		/*
		return new byte[] { 
			(byte) ((a >> 24) & 0xFF), 
			(byte) ((a >> 16) & 0xFF), 
			(byte) ((a >> 8) & 0xFF),
			(byte) (a & 0xFF) 
		};
		*/
	}

	public static byte int2Byte(int x) {
		return (byte) x;
	}

	public static byte[] long2Bytes(long a) {
		byte[] bs = new byte[8];
		for (int i = 0; i < 8; i++) {
			bs[i] = (byte)((a >> ((7-i) * 8)) & 0xFF);
		}
		return bs;
		/*
		ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
		buffer.putLong(0, a);
		return buffer.array();
		*/
	}

	private static int parse(char c) {
		if (c >= 'a')
			return (c - 'a' + 10) & 0x0f;
		if (c >= 'A')
			return (c - 'A' + 10) & 0x0f;
		return (c - '0') & 0x0f;
	}

	private static String hex2ByteLength(String str, int len) {
		for (int j = str.length(); j < len * 2; j++) {
			str = "0" + str;
		}
		return str;
	}

}
